package com.sytc.camera.util;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

@Configuration(proxyBeanMethods = false)
@PropertySource("classpath:appConfig.yaml") // 指定自定义的 YAML 文件
@ConfigurationProperties(prefix = "")  // 加载没有前缀的配置，这句一定要要，要不然加载不到
public class AppConfig {

    private static final Logger log = LoggerFactory.getLogger(AppConfig.class);  // 创建日志记录器

    // 获取 JavaScript 引擎
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");


    private String firmwarePath;
    private String cyapiDllPath;
    private String outPath;
    private byte[] imagePacketHead;
    private byte[] imagePacketTail;
    private int imageWidth;
    private int imageHeight;
    private int transferSize;
    private int frontRemainSize;
    private int backRemainSize;
    private int queueSize;
    private long maxPauseMilliSec;

    public long getMaxPauseMilliSec() {
        return maxPauseMilliSec;
    }

    public void setMaxPauseMilliSec(long maxPauseMilliSec) {
        this.maxPauseMilliSec = maxPauseMilliSec;
    }

    public String getOutPath() {
        return outPath;
    }

    public void setOutPath(String outPath) {
        this.outPath = outPath;
    }

    public int getFrontRemainSize() {
        return frontRemainSize;
    }

    public void setFrontRemainSize(int frontRemainSize) {
        this.frontRemainSize = frontRemainSize;
    }

    public int getBackRemainSize() {
        return backRemainSize;
    }

    public void setBackRemainSize(int backRemainSize) {
        this.backRemainSize = backRemainSize;
    }

    public int getQueueSize() {
        return queueSize;
    }

    public void setQueueSize(int queueSize) {
        this.queueSize = queueSize;
    }


    public String getCyapiDllPath() {
        return cyapiDllPath;
    }

    public void setCyapiDllPath(String cyapiDllPath) {
        this.cyapiDllPath = cyapiDllPath;
    }

    public int getImageWidth() {
        return imageWidth;
    }

    public void setImageWidth(int imageWidth) {
        this.imageWidth = imageWidth;
    }

    public int getImageHeight() {
        return imageHeight;
    }

    public void setImageHeight(int imageHeight) {
        this.imageHeight = imageHeight;
    }

    public String getFirmwarePath() {
        return firmwarePath;
    }

    public void setFirmwarePath(String firmwarePath) {
        this.firmwarePath = firmwarePath;
        log.info("加载到的配置 firmwarePath 为：{}", firmwarePath);  // 添加日志
    }

    public byte[] getImagePacketHead() {
        return imagePacketHead;
    }

    public void setImagePacketHead(String imagePacketHead) {
        try {
            this.imagePacketHead = Hex.decodeHex(imagePacketHead.replaceAll("\\s+", ""));
            log.info("加载到的配置 usbPacketHead 为：{}", imagePacketHead);  // 添加日志
        } catch (DecoderException e) {
            Util.errorAlert("加载配置文件失败，请检查配置usbPacketHeader");
        }
    }

    public byte[] getImagePacketTail() {
        return imagePacketTail;
    }

    public void setImagePacketTail(String imagePacketTail) {
        try {
            this.imagePacketTail = Hex.decodeHex(imagePacketTail.replaceAll("\\s+", ""));
            log.info("加载到的配置 usbPacketTail 为：{}", imagePacketTail);  // 添加日志
        } catch (DecoderException e) {
            Util.errorAlert("加载配置文件失败，请检查配置usbPacketFooter");
        }
    }

    public int getTransferSize() {
        return transferSize;
    }

    public void setTransferSize(String transferSize) {
        try {
            this.transferSize = ((Number) engine.eval(transferSize)).intValue();
            log.info("加载到的配置 transferSize 为：{}", this.transferSize);  // 添加日志
        } catch (ScriptException e) {
            Util.errorAlert("加载配置文件失败，请检查配置");
        }
    }
}
